package org.example.springbook.controller;


import lombok.extern.slf4j.Slf4j;
import org.example.springbook.enums.BookStatusEnum;
import org.example.springbook.model.BookInfo;
import org.example.springbook.model.PageRequest;
import org.example.springbook.model.PageResponse;
import org.example.springbook.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.transform.Result;
import java.util.List;

@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping("/addBook")
    public String addBook(BookInfo bookInfo){
        log.info("添加图书, bookinfo:{}", bookInfo);

        //1.参数校验
        if (!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount()==null
                || bookInfo.getPrice()==null
                || !StringUtils.hasLength(bookInfo.getPublish())
        ){
            return "参数不合法, 请重新检查";
        }
        Integer result = bookService.insertBook(bookInfo);
        try {
            if(result == 1){
                return "";
            }
        } catch (Exception e) {
            log.error("数据插入发生异常, e:", e);
        }
        return "数据插入失败, 请联系管理员";
    }

    /**
     * 查询图书信息(翻页使用)
     */
    @RequestMapping("/getListByPage")
    public PageResponse<BookInfo> getListByPage(PageRequest pageRequest){
        log.info("获取图书列表, pageRequest: {}", pageRequest);
        //参数校验省略
        PageResponse<BookInfo> bookInfoPageResponse = bookService.getListByPage(pageRequest);
        return bookInfoPageResponse;
    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer id, Integer bookId){
        Integer realId = bookId != null ? bookId : id;
        log.info("获取图书信息, id:" + realId);
        return bookService.queryBookById(realId);
    }

    @RequestMapping("/updateBook")
    public String updateBook(BookInfo bookInfo){
        log.info("更新图书, bookinfo:{}", bookInfo);
        if (bookInfo.getId() == null){
            return "缺少图书ID";
        }
        try{
            Integer result = bookService.updateBook(bookInfo);
            if(result != null && result > 0){
                return "";
            }
        }catch (Exception e){
            log.error("更新发生异常, e:", e);
        }
        return "更新失败, 请联系管理员";
    }

    @RequestMapping("/deleteBook")
    public String deleteBook(Integer bookId){
        log.info("删除图书, bookId: {}", bookId);
        try {
            BookInfo bookInfo = new BookInfo();
            bookInfo.setId(bookId);
            bookInfo.setStatus(BookStatusEnum.DELETED.getCode());
            Integer result = bookService.updateBook(bookInfo);
            if (result>0){
                return "";
            }
            return "图书删除失败";
        }catch (Exception e){
            log.error("删除图书失败, e:", e);
            return "数据库操作失败";
        }
    }

    /**
     * 批量删除图书
     * @param ids
     * @return
     */
    @RequestMapping("/batchDeleteBook")
    public Boolean batchDeleteBook(@RequestParam List<Integer> ids){
        log.info("批量删除图书, ids:{}", ids);
        try {
            //执行SQL
            bookService.batchDelete(ids);
            return true;
        }catch (Exception e){
            log.error("批量删除图书失败, ids:{}", ids);
        }
        return false;

    }
}

























